﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display {
    import flash.display.*;
    import flash.geom.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import com.greensock.*;
    import flash.filters.*;
    import flash.text.*;
    import com.greensock.easing.*;

    public class StreakBonus extends GameSprite {

        public static const STREAKBONUS_REWARDTEXT_XOFFSET:int = 50;
        public static const STREAKBONUS_XOFFSET:int = 0;
        public static const STREAKBONUS_YOFFSET:int = 55;

        protected const STREAKBONUS_REWARDTEXT_TIME:int = 2000;
        protected const STREAKBONUS_REWARD_DELAY:Number = 300;
        protected const STREAKBONUS_FONT_SIZE:Number = 16;
        protected const STREAKBONUS_REWARD_FONT_SIZE:Number = 24;

        protected var m_xml:XML;
        protected var m_progressBarBack:DisplayObject;
        protected var m_progressBar:DisplayObject;
        protected var m_originalProgressBarWidth:int;
        protected var m_expireTime:Number;
        protected var m_warningTime:Number;
        protected var m_activeElapsedTime:Number;
        protected var m_isActive:Boolean;
        protected var m_isDisplayUpdating:Boolean;
        protected var m_inWarningPhase:Boolean;
        protected var m_streakMsgs:Array;
        protected var m_rewardMsg:TextField;
        protected var m_maxesReached:int;
        protected var m_tierReached:int;
        protected var m_maxTiers:int;
        protected var m_rewardMultiplier:Number;
        protected var m_warningBitmap:Bitmap;
        protected var m_warningBitmapData:BitmapData;
        protected var m_soundLoaded:Boolean = false;
        protected var m_streakSounds:int = 6;
        protected var m_current_bonus:Number = 0;
        protected var m_current_progress:Number = 0;
        protected var m_displayed_progress:Number = 0;
        protected var m_rewardAmounts:Array;
        private var m_bonusAmountsQueue:Array;
        private var m_progressMaxed:Boolean = false;

        public function StreakBonus(_arg1:XML){
            var _local3:DisplayObject;
            this.m_streakMsgs = [];
            this.m_warningBitmap = new Bitmap();
            this.m_rewardAmounts = new Array();
            this.m_bonusAmountsQueue = new Array();
            super();
            this.m_xml = _arg1;
            this.m_progressBarBack = new EmbeddedArt.streakFrame();
            this.m_progressBar = new EmbeddedArt.streakBar();
            this.m_progressBar.x = 8;
            this.m_progressBar.y = 5;
            this.m_originalProgressBarWidth = this.m_progressBar.width;
            var _local2:XMLList = _arg1.tier;
            this.m_maxTiers = _local2.length();
            this.m_expireTime = _arg1.@expire_time;
            this.m_warningTime = _arg1.@warning_time;
            this.m_rewardMultiplier = _arg1.@multiplier;
            this.initStreakMessages();
            this.addChild(this.m_warningBitmap);
            this.addChild(this.m_progressBarBack);
            this.addChild(this.m_progressBar);
            this.hitTestObject(this.m_progressBarBack);
            for each (_local3 in this.m_streakMsgs) {
                this.addChild(_local3);
            };
            this.m_progressBar.width = 0;
            this.reset();
        }
        protected function initStreakMessages():void{
            var _local2:TextField;
            var _local1 = 1;
            while (_local1 <= this.m_maxTiers) {
                _local2 = this.createStreakMessage(ZLoc.t("Dialogs", ("StreakMessage" + _local1)), true, this.STREAKBONUS_FONT_SIZE);
                this.m_streakMsgs[(_local1 - 1)] = _local2;
                _local1++;
            };
        }
        private function createStreakMessage(_arg1:String, _arg2:Boolean, _arg3:Number):TextField{
            var _local4:TextField = new TextField();
            _local4.text = _arg1;
            var _local5:TextFormat = new TextFormat();
            _local5.font = EmbeddedArt.TITLE_FONT;
            _local5.size = _arg3;
            _local5.color = 2193847;
            _local4.setTextFormat(_local5);
            _local4.filters = [new GlowFilter(0xFFFFFF, 1, 5, 5, 15)];
            _local4.embedFonts = true;
            _local4.selectable = false;
            _local4.mouseEnabled = false;
            _local4.cacheAsBitmap = _arg2;
            _local4.autoSize = TextFieldAutoSize.LEFT;
            _local4.alpha = 0;
            _local4.scaleX = 0;
            _local4.scaleY = 0;
            return (_local4);
        }
        protected function turnOnStreakMessage(_arg1:DisplayObject, _arg2:Number=0, _arg3:Number=0):void{
            var _local4:DisplayObject;
            var _local5:int;
            if (!_arg1){
                return;
            };
            for each (_local4 in this.m_streakMsgs) {
                if (_local4.alpha){
                    this.turnOffStreakMessage(_local4);
                };
            };
            _arg1.scaleX = 1;
            _arg1.scaleY = 1;
            _arg1.x = ((this.m_progressBarBack.width / 2) + _arg2);
            _arg1.y = (10 + _arg3);
            _local5 = (-(_arg1.width) / 2);
            _arg1.scaleX = 0;
            _arg1.scaleY = 0;
            TweenLite.to(_arg1, 0.2, {
                scaleX:1,
                scaleY:1,
                x:((_local5 + (this.m_progressBarBack.width / 2)) + _arg2),
                alpha:1
            });
        }
        protected function turnOffStreakMessage(_arg1:DisplayObject, _arg2:Boolean=false):void{
            var streakMsg:* = _arg1;
            var removeMsg:Boolean = _arg2;
            TweenLite.to(streakMsg, 0.2, {
                alpha:0,
                onComplete:function ():void{
                    streakMsg.scaleX = 0;
                    streakMsg.scaleY = 0;
                    if (((removeMsg) && (streakMsg.parent))){
                        streakMsg.parent.removeChild(streakMsg);
                        streakMsg = null;
                    };
                }
            });
        }
        protected function warningTimeReached():void{
            var _local1:GlowFilter;
            if (!this.m_inWarningPhase){
                this.m_inWarningPhase = true;
                this.m_warningBitmapData = new BitmapData(this.m_progressBarBack.width, this.m_progressBarBack.height, true, 0);
                this.m_warningBitmapData.draw(this);
                this.m_warningBitmap.bitmapData = this.m_warningBitmapData;
                _local1 = new GlowFilter(0xFF0000, 0.8, 15, 15, 5);
                this.m_warningBitmap.filters = [_local1];
                this.toggleBlink();
            };
        }
        protected function toggleBlink():void{
            if (!this.m_inWarningPhase){
                this.m_warningBitmap.alpha = 0;
                return;
            };
            if (this.m_warningBitmap.alpha){
                TweenLite.to(this.m_warningBitmap, 0.2, {
                    alpha:0,
                    onComplete:function ():void{
                        toggleBlink();
                    }
                });
            } else {
                TweenLite.to(this.m_warningBitmap, 0.2, {
                    alpha:0.8,
                    onComplete:function ():void{
                        toggleBlink();
                    }
                });
            };
        }
        protected function reset():void{
            this.alpha = 0;
            this.visible = false;
            this.mouseEnabled = false;
            this.mouseChildren = false;
            this.m_current_bonus = 0;
            this.m_current_progress = 0;
            this.m_displayed_progress = 0;
            this.m_maxesReached = 0;
            this.m_tierReached = 0;
            this.m_progressMaxed = false;
            this.m_activeElapsedTime = 0;
            this.m_isActive = false;
            this.m_isDisplayUpdating = false;
            this.m_warningBitmap.alpha = 0;
            this.turnOnStreakMessage(this.m_streakMsgs[0]);
            this.m_inWarningPhase = false;
            this.m_rewardAmounts.splice(0, this.m_rewardAmounts.length);
        }
        protected function expireTimeReached():void{
            this.m_isActive = false;
            if (this.m_maxesReached > 0){
                this.rewardStreakBonus();
                Sounds.play("doober_coin_click");
            } else {
                TweenLite.to(this, 0.2, {
                    alpha:0,
                    onComplete:function ():void{
                        reset();
                    }
                });
            };
        }
        protected function resetExpireTimeAndShow():void{
            this.m_inWarningPhase = false;
            this.m_isActive = true;
            this.m_activeElapsedTime = 0;
            TweenLite.to(this, 0.5, {alpha:1});
        }
        protected function stopWarning():void{
            this.m_inWarningPhase = false;
        }
        public function incProgress(_arg1:Number):void{
            this.visible = true;
            this.mouseEnabled = true;
            this.enableDisplayUpdating();
            this.resetExpireTimeAndShow();
            if (this.m_rewardMsg){
                this.turnOffStreakMessage(this.m_rewardMsg, true);
            };
            if (((!(this.m_progressMaxed)) && ((this.m_tierReached < this.m_maxTiers)))){
                this.m_current_bonus = (this.m_current_bonus + _arg1);
                this.m_current_progress = (this.m_current_progress + this.bonusPerClick);
                if (this.m_current_progress >= this.calculateMaxProgress()){
                    this.m_tierReached = (this.m_tierReached + 1);
                    this.m_progressMaxed = true;
                };
            } else {
                this.m_bonusAmountsQueue.push(_arg1);
            };
        }
        protected function maxProgressReached():void{
            var _local1:Number;
            this.m_maxesReached = (this.m_maxesReached + 1);
            this.m_current_bonus = 0;
            this.m_current_progress = 0;
            this.m_displayed_progress = 0;
            if (this.m_maxesReached <= this.m_streakSounds){
                Sounds.play(("bonus_ramp_" + this.m_maxesReached));
            } else {
                if (this.m_maxesReached > this.m_streakSounds){
                    Sounds.play(("bonus_ramp_" + (this.m_maxesReached - this.m_streakSounds)));
                };
            };
            if (this.m_maxesReached >= this.m_maxTiers){
                this.rewardStreakBonus();
            } else {
                this.turnOnStreakMessage(this.m_streakMsgs[this.m_maxesReached]);
            };
            this.m_progressMaxed = false;
            while ((((((this.m_bonusAmountsQueue.length > 0)) && (!(this.m_progressMaxed)))) && ((this.m_tierReached < this.m_maxTiers)))) {
                _local1 = this.m_bonusAmountsQueue.shift();
                this.m_current_bonus = (this.m_current_bonus + _local1);
                this.m_current_progress = (this.m_current_progress + this.bonusPerClick);
                if (this.m_current_progress >= this.calculateMaxProgress()){
                    this.m_tierReached = (this.m_tierReached + 1);
                    this.m_progressMaxed = true;
                    break;
                };
            };
        }
        public function rewardStreakBonus():void{
            var timeDelay:* = 0;
            var totalReward:* = this.totalBonus;
            this.m_rewardMsg = this.createStreakMessage(ZLoc.t("Dialogs", "StreakRewardCoin", {amount:totalReward}), false, this.STREAKBONUS_REWARD_FONT_SIZE);
            this.stopWarning();
            Global.hud.addChild(this.m_rewardMsg);
            this.turnOnStreakMessage(this.m_rewardMsg, ((this.x - this.width) + STREAKBONUS_REWARDTEXT_XOFFSET), this.y);
            this.addRewardToPlayer(totalReward);
            GameTransactionManager.addTransaction(new TStreakBonus(totalReward, this.m_tierReached));
            this.reset();
            TimerUtil.callLater(function ():void{
                TweenLite.to(this, 0.2, {
                    alpha:0,
                    onComplete:function ():void{
                        turnOffStreakMessage(m_rewardMsg, true);
                    }
                });
            }, this.STREAKBONUS_REWARDTEXT_TIME);
        }
        protected function animateRewardTextFlyout(_arg1:Number, _arg2:Number, _arg3:Boolean=false, _arg4:TextField=null):void{
            var amount:* = _arg1;
            var timeDelay:* = _arg2;
            var resetBar:Boolean = _arg3;
            var rewardMsg = _arg4;
            TimerUtil.callLater(function ():void{
                var spr:* = null;
                spr = new Sprite();
                var statusText:* = new StrokeTextField(0x202020, 0.8);
                statusText.embedFonts = true;
                var resource_fmt:* = new TextFormat(EmbeddedArt.DEFAULT_FONT_NAME_BOLD, STREAKBONUS_FONT_SIZE, null, true);
                resource_fmt.color = 4106655;
                statusText.defaultTextFormat = resource_fmt;
                statusText.toolTipText = ("+" + amount);
                spr.addChild(statusText);
                spr.x = (Global.hud.streakBonus.midPointX - (statusText.width / 2));
                spr.y = (Global.hud.streakBonus.midPointY - (statusText.height / 2));
                Global.hud.addChild(spr);
                var coinsHolderPoint:* = new Point(Global.hud.getComponent["coins"].x, Global.hud.getComponent["coins"].y);
                var bezCtrlPointX:* = (((spr.x - coinsHolderPoint.x) / 2) + coinsHolderPoint.x);
                var bezCtrlPointY:* = (spr.y + 200);
                TweenLite.to(spr, 2, {
                    bezier:[{
                        x:bezCtrlPointX,
                        y:bezCtrlPointY
                    }, {
                        x:coinsHolderPoint.x,
                        y:coinsHolderPoint.y
                    }],
                    scaleX:1.5,
                    scaleY:1.5,
                    ease:Quart.easeOut,
                    orientToBezier:false,
                    onComplete:function ():void{
                        if (spr.parent){
                            spr.parent.removeChild(spr);
                        };
                        addRewardToPlayer(amount);
                        if (resetBar){
                            if (rewardMsg){
                                turnOffStreakMessage(rewardMsg, true);
                            };
                            TweenLite.to(this, 0.2, {
                                alpha:0,
                                onComplete:function ():void{
                                    reset();
                                }
                            });
                        };
                    }
                });
            }, timeDelay);
        }
        protected function addRewardToPlayer(_arg1:Number):void{
            Global.player.gold = (Global.player.gold + _arg1);
        }
        protected function calculateMaxProgress():Number{
            return (Number(this.m_xml.tier[this.m_maxesReached].@threshold));
        }
        protected function get bonusPerClick():Number{
            return (Number(this.m_xml.tier[this.m_maxesReached].@bonus_per_click));
        }
        protected function calculateBonusForCurrentTier():Number{
            var _local1:Number = this.m_xml.tier[this.m_maxesReached].@reward_value;
            return (Math.ceil(((Global.player.level * this.m_rewardMultiplier) * _local1)));
        }
        public function get totalBonus():Number{
            var _local1:Number = this.m_xml.tier[(this.m_tierReached - 1)].@reward_value;
            return (Math.ceil(((Global.player.level * this.m_rewardMultiplier) * _local1)));
        }
        protected function enableDisplayUpdating():void{
            this.m_isDisplayUpdating = true;
        }
        protected function disableDisplayUpdating():void{
            this.m_isDisplayUpdating = false;
        }
        protected function updateStreakBonus():void{
            if (this.m_displayed_progress >= this.m_current_progress){
                this.disableDisplayUpdating();
            };
            this.m_displayed_progress = (this.m_displayed_progress + ((this.calculateMaxProgress() * 2) / 100));
            this.updateFrame();
        }
        public function updateFrame():void{
            if (this.alpha == 0){
                TweenLite.to(this, 0.2, {alpha:1});
            };
            this.m_progressBar.width = ((this.m_displayed_progress / this.calculateMaxProgress()) * this.m_originalProgressBarWidth);
            var _local1:Number = this.calculateMaxProgress();
            if (this.m_displayed_progress >= _local1){
                this.maxProgressReached();
            };
        }
        public function onUpdate(_arg1:Number):void{
            if (((this.m_isActive) && (!(UI.isModalDialogOpen)))){
                this.m_activeElapsedTime = (this.m_activeElapsedTime + _arg1);
                if (this.m_isDisplayUpdating){
                    this.updateStreakBonus();
                };
                if (this.m_activeElapsedTime >= this.m_warningTime){
                    this.warningTimeReached();
                };
                if (this.m_activeElapsedTime >= this.m_expireTime){
                    this.expireTimeReached();
                };
            };
        }
        public function get midPointX():Number{
            return ((this.x + (this.m_progressBarBack.width / 2)));
        }
        public function get midPointY():Number{
            return ((this.y + (this.m_progressBarBack.height / 2)));
        }

    }
    var _local1:* = TweenPlugin.activate([BezierPlugin]);
    return (_local1);
}//package Display 
